package com.ymx.darling.generater.def;

import java.util.Map;
import java.util.StringJoiner;

/**
 * 实体Def内容构造器
 * @author 爱Java的项目
 */
public class ContentBuilder {
    public static String buildTableDef(String packageName, String className, String tableName, Map<String, String> propertyAndColumnMap, NamingStyle namingStyle) {
        StringBuilder tableDefBuilder = new StringBuilder();
        StringJoiner defaultColumns = new StringJoiner(", ");
        tableDefBuilder.append("package ").append(packageName).append(";\n\n");
        tableDefBuilder.append("import com.ymx.darling.util.StringUtil;\n");
        tableDefBuilder.append("import com.ymx.darling.wrapper.fragment.TableInfo;\n\n");
        tableDefBuilder.append("public class ").append(className).append(" implements TableInfo {\n");
        tableDefBuilder.append(String.format("\tprivate static final String TABLE_NAME = \"%s\";\n\n", tableName));
        tableDefBuilder.append(String.format("\tpublic static final %s %s = %s.as(\"\");\n\n", className, NamingConverter.convertNamingStyle(tableName, namingStyle), className));
        tableDefBuilder.append("\tprivate final String currentTableName;\n\n");
        tableDefBuilder.append("\tprivate final String aliaName;\n\n");
        propertyAndColumnMap.keySet().forEach(property -> {
            String columnName = NamingConverter.convertNamingStyle(property, namingStyle);
            defaultColumns.add(columnName);
            tableDefBuilder.append(String.format("\tpublic final String %s;\n\n", columnName));
        });
        tableDefBuilder.append(String.format("\tpublic final String[] %s;\n\n", NamingConverter.convertNamingStyle("defaultColumns", namingStyle)));
        // 拼接构造方法
        tableDefBuilder.append(String.format("\tprivate %s(String aliaName) {\n", className));
        tableDefBuilder.append("\t\tthis.aliaName = aliaName;\n");
        tableDefBuilder.append("\t\tif (StringUtil.isNull(aliaName)) {\n");
        tableDefBuilder.append("\t\t\taliaName = TABLE_NAME;\n");
        tableDefBuilder.append("\t\t\tcurrentTableName = TABLE_NAME;\n");
        tableDefBuilder.append("\t\t} else {\n");
        tableDefBuilder.append("\t\t\tcurrentTableName = TABLE_NAME + \" AS \" + aliaName;\n");
        tableDefBuilder.append("\t\t}\n");
        propertyAndColumnMap.forEach((property, column) ->
                tableDefBuilder.append(String.format("\t\tthis.%s = aliaName + \".\" + \"%s\";\n", NamingConverter.convertNamingStyle(property, namingStyle), column))
        );
        tableDefBuilder.append(String.format("\t\tthis.%s = new String[]{%s};\n", NamingConverter.convertNamingStyle("defaultColumns", namingStyle), defaultColumns));
        tableDefBuilder.append("\t}\n\n");
        // 构建as方法
        tableDefBuilder.append(String.format("\tpublic static %s as(String aliaName) {\n", className));
        tableDefBuilder.append(String.format("\t\treturn new %s(aliaName);\n", className));
        tableDefBuilder.append("\t}\n\n");
        // 构建getTableName方法
        tableDefBuilder.append("\t@Override\n");
        tableDefBuilder.append("\tpublic String getTableName() {\n");
        tableDefBuilder.append("\t\treturn this.currentTableName;\n");
        tableDefBuilder.append("\t}\n\n");
        // 构建getAliaName方法
        tableDefBuilder.append("\t@Override\n");
        tableDefBuilder.append("\tpublic String getAliaName() {\n");
        tableDefBuilder.append("\t\treturn this.aliaName;\n");
        tableDefBuilder.append("\t}\n\n");
        // 类结束
        tableDefBuilder.append("}");

        return tableDefBuilder.toString();
    }
}
